/***********************************************************************
 * Module:  VehicleController.java
 * Author:  Administrator
 * Purpose: Defines the Class VehicleController
 ***********************************************************************/

package ishitong.vehicleManagement.controller;

import ishitong.common.SysCodeType;
import ishitong.common.model.SysCode;
import ishitong.vehicle.common.model.Vehicle;
import ishitong.vehicleManagement.service.biz.impl.SPIVehicleService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/** @pdOid 303cb6f7-5f5c-481c-a2cf-f68bc5d35399 */
@Controller
public class VehicleController extends ishitong.common.controller.ActionController {
   /** @pdOid cfdc6ba3-4f95-4b07-87fd-4abe5ce890e2 */
   private static final String VHC_SLT_DLG = "vehicleManagement/vehicle/vehicleSelectDialog";
   /** @pdOid a5e37c83-d556-414d-9261-b7a22c99b1bb */
   private static final String VHC_SLT_LST = "vehicleManagement/vehicle/vehicleSelectList";
   /** @pdOid c83bcd73-3633-40f4-be76-472ebe00a8f6 */
   private static final String VHC_MGT_EDT = "vehicleManagement/vehicle/vehicleCommonView";
   /** @pdOid f8d4b9de-8156-4371-a3c1-af66aa7fb8c9 */
   @Autowired
   private SPIVehicleService spiVehicleService;
   
   /** @param request
    * @pdOid 2c4c8f06-e12a-4a05-b4b4-efc031e6c75b */
   @RequestMapping(value = "/ajaxShowVehicleSelectDialog.do",method = RequestMethod.POST)
   public ModelAndView handleAjaxShowVehicleSelectDialog(HttpServletRequest request) {
   	try {
   		Map<String, Object> model = new HashMap<>();
   
   		// 号牌种类
   		List<SysCode> lstLicencePlateKind = spiSysCodeService.getSysCodeListByTypeCode(SysCodeType.PLATE_KIND); 
   		model.put("lstLicencePlateKind", lstLicencePlateKind);
   
   		//车辆状态
   		List<SysCode> lstVehicleStates = spiSysCodeService.getSysCodeListByTypeCode(SysCodeType.VEHICLE_STATES);
   		model.put("lstVehicleStates", lstVehicleStates);
   		return new ModelAndView(VHC_SLT_DLG, model);
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return null;
   }
   
   /** @param request 
    * @param vehicle
    * @pdOid bc543e55-f356-4372-acbd-d001891fafca */
   @RequestMapping(value = "/ajaxFindVehicleSelectList.do",method = RequestMethod.POST)
   public ModelAndView handleAjaxFindVehicleSelectList(HttpServletRequest request, Vehicle vehicle) {
   	try {
   		Map<String, Object> model = new HashMap<>();
   		List<Vehicle> listVehicle = spiVehicleService.findByQuery(vehicle);
   		model.put("listVehicle", listVehicle);
   		return new ModelAndView(VHC_SLT_LST, model);
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return null;
   }
   
   /** @param request 
    * @param vehicle
    * @pdOid f0a35b8d-3a0d-47cc-8743-7dae0e0d81ad */
   @RequestMapping(value = "/ajaxFindVehicleCommon.do",method = RequestMethod.POST)
   public ModelAndView ajaxFindVehicleCommon(HttpServletRequest request, Vehicle vehicle) {
   	try {
   		Map<String, Object> model = new HashMap<>();
   		SysCode sysCode=spiSysCodeService.getSysCodeById(vehicle.getPlatetype());
   		vehicle.setPlatetype(sysCode.getCode());
   		if (!StringUtils.isEmpty(vehicle.getPlatenum())||!StringUtils.isEmpty(vehicle.getPlatetype())) {
   			List<Vehicle> listVehicle = spiVehicleService.findByQuery(vehicle);
   			if(listVehicle.size()>0){
   	   			model.put("vehicle",listVehicle.get(0));
   			}
   		}
   		return new ModelAndView(VHC_MGT_EDT, model);
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return null;
   }

}